#include <iostream>
#include <string>
using namespace std;

struct node
{
	string name;
	float salary;
	node* next;  
};

void insert(node* &head, string newname, float newsalary)
{	
	node* newnode = new node;
	newnode->name = newname;
	newnode->salary = newsalary;

	newnode->next = head;
	head = newnode;	
}

void display(node* head)
{
	node* pread = head;
	cout << "The link list is: \n";
	while(pread != NULL)
	{
		cout << "Name: " << pread->name << "  " << "Salary: " << pread->salary << endl;
		pread = pread->next;
	}
	cout << endl;
}

void clear(node* &head)//delete link list function.????. 
{
	node*p;//define to be deleted node.
	while (head != NULL)
	{
		p = head;
		head = head->next;
		delete p;
	}
	cout << "the link list han been deleted!" << endl;
}
//
//Salary as a descending order,exchange the value of the two nodes to achieve sort.
//
void downsort(node* &head)
{
	node* befor_make = head;
	for (node* make = head; make != NULL; make = make->next)
	{
		node* befor_curr = make;	// 
		for (node* curr = make->next; curr != NULL; curr = curr->next)
		{
			//node* befor_curr = head;	
			if (curr->salary > make->salary)
			{
				if (make == head)
				{
					if (make == befor_curr)
					{
						make->next = curr->next;
						curr->next = head;

						head = curr;
						//curr = make->next;
						curr = make;	// 
						make = head;	// 

					}
					else
					{
						befor_curr->next = curr->next;
						curr->next = make->next;
						make->next = befor_curr->next;
						befor_curr->next = make;

						head = curr;	// 
						make = curr;
						curr = befor_curr->next;
					}

				}
				else
				{                               
					if (make == befor_curr)
					{
						make->next = curr->next;
						curr->next = make;
						//curr = befor_make->next;	
						befor_make->next = curr;	// 

						curr = make;
						make = befor_make->next;	// 
					}
					else
					{
						befor_curr->next = curr->next;
						curr->next = make->next;
						make->next = befor_curr->next;
						befor_curr->next = make;
						//curr = befor_make->next;	
						befor_make->next = curr;

						make = curr;
						curr = befor_curr->next;
					}
				}
			}
			befor_curr = curr;
		}
		befor_make = make;
	}
} 

int main()
{
	node* head = NULL;
	insert(head, "Jimmy", 1250.04);
	insert(head, "Tim", 1280.07);
	insert(head, "Rose", 1390.08);
	insert(head, "Tommy", 1490.35);
	insert(head, "Jon", 1320.54);
	display(head);

	cout << "Salary as a descending order : \n";
	downsort(head);
	display(head);

	clear(head);

	system("pause");
	return 0;
}
